package edu.whu.puppygo.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import edu.whu.puppygo.config.OssConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guanruwang
 * @date 2023/11/14$
 */
@Component
@Slf4j
public class OssUtil {
    private OSS ossClient = OssConfig.initOSSClient();

    public String printOssInfo() {
        return "OssUtil{" + OssConfig.getBucketName() +
                '}';
    }

    public String uploadFile(MultipartFile file, String filename) {
        String fileName = "";
        try {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(inputStream.available());
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            objectMetadata.setContentType(file.getContentType());
            objectMetadata.setContentDisposition("inline;filename=" + filename);
            fileName = filename;
            // 上传文件
            ossClient.putObject(OssConfig.getBucketName(), fileName, inputStream, objectMetadata);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return fileName;
    }


    public List<String> listObjects() {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(OssConfig.getBucketName()).withMaxKeys(200);
        ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
        List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        return objectSummaries.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList());
    }


    public boolean doesObjectExist(String fileName) {
        try {
            if (Strings.isEmpty(fileName)) {
                System.out.println("文件名不能为空");
                return false;
            } else {
                return ossClient.doesObjectExist(OssConfig.getBucketName(), fileName);
            }
        } catch (OSSException | ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void exportFile(OutputStream os, String objectName) {

        OSSObject ossObject = ossClient.getObject(OssConfig.getBucketName(), objectName);
        BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
        BufferedOutputStream out = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int lenght;
        try {
            while ((lenght = in.read(buffer)) != -1) {
                out.write(buffer, 0, lenght);
            }
            out.flush();
        } catch (IOException e) {
            log.error("Error occurred: {}", e.getMessage(), e);
        }

    }

}
